package com.itcam.lambda.pro01lambda;

import com.itcam.lambda.pro01lambda.interface_.Factory;
import com.itcam.lambda.pro01lambda.interface_.impl.SubClass;
import com.itcam.lambda.pro01lambda.entity.User;

/**
 * @author : Cammy.Wu
 * Description : 
 */

public class LambdaTest {

    public static void main(String[] args) {
        // 1.子类实现接口
        Factory factory = new SubClass();
        User user = (User) factory.getObject();
        System.out.println("user = " + user);

        // 2.匿名内部类
        factory = new Factory() {
            @Override
            public Object getObject() {
                return new User("张三", 22);
            }
        };
        User user1 = (User) factory.getObject();
        System.out.println("user1 = " + user1);

        // 3. lambda表达式
        factory = () -> {
            return new User("李四", 23);
        };
        User user2 = (User) factory.getObject();
        System.out.println("user2 = " + user2);

        // 4. lambda作为参数进行传递
        User user3 = getUserFormFactory(() -> {
            return new User("王五", 24);
        }, "User");
    }

    public static User getUserFormFactory(Factory factory, String beanName) {
        Object object = factory.getObject();
        if (object != null && object.getClass().getSimpleName().equals(beanName)) {
            return (User) object;
        }
        return null;
    }
}
